package cn.xiaolang.function.file_manage;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

/**
 * Base RequestHandler
 *
 * @author Dean
 * @date 2020-12-15
 */
@Slf4j
public class HttpsDemo {

    @Getter
    private static final RestTemplate REST_OPERATION;

    /**
     * 获取证书
     *
     * @return
     */
    private static KeyStore getkeyStore(String type, String filePath, String password) {
        KeyStore keySotre = null;
        try (FileInputStream in = new FileInputStream(new File(filePath))) {
            keySotre = KeyStore.getInstance(type);
            keySotre.load(in, password.toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return keySotre;
    }

    static {
        SSLContext sslContext = null;
        try {

            sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> false).build();
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");

            //获取信任证书库
            KeyStore trustStore = getkeyStore("jks", "/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre/lib/security/cacerts", "123456");
            trustManagerFactory.init(trustStore);
            TrustManager[]  trustManagers = trustManagerFactory.getTrustManagers();
            sslContext.init(null,trustManagers,null);

        } catch (Exception e) {
            log.error("init http client for ssl exception:", e);
        }
        Assert.notNull(sslContext, "build sslContext exception");

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
                new String[]{"TLSv1"},
                null,
                NoopHostnameVerifier.INSTANCE);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        REST_OPERATION = new RestTemplate(requestFactory);
    }

    public static String get(String url) {
        return REST_OPERATION.getForObject(url, String.class);
    }

    public static void main(String[] args) {
        String url = "https://api.us.xiaoyi.com:8443/v2/ipc/sync_time?hmac=BIke7GBrJXU6Qe41glDqGUkYNp8%3D&seq=9";
        System.out.println(get(url));
    }

}
